import {ChangeDetectorRef, Component, EventEmitter, Input, OnDestroy, OnInit, Output} from '@angular/core';
import {ExamsManageService} from '../../../../core/services/http/exams-manage/exams-manage.service';
import {Router} from '@angular/router';
import {WindowService} from '../../../../core/services/common/window.service';

interface ExamsDetail {
  name: string;
  duration: number;
  countScore: number;
  remark?: string;
  passScore: number;
}

@Component({
  selector: 'app-detail-online-exam',
  templateUrl: './detail-online-exam.component.html'
})
export class DetailOnlineExamComponent implements OnInit, OnDestroy {
  @Input() currentPageNum!: number;
  @Output() returnBack: EventEmitter<any>;
  @Input() id!: number;
  dataInfo: ExamsDetail = {
    name: '',
    duration: -1,
    countScore: -1,
    remark: '',
    passScore: -1
  };


  constructor(private dataService: ExamsManageService, private router: Router, private cdr: ChangeDetectorRef,
              private windowServe: WindowService) {
    this.returnBack = new EventEmitter<any>();
  }


  getDetail(): void {
    this.dataService.getExamsManageDetail(this.id).subscribe(det => {
      this.dataInfo = {
        name: det.name,
        duration: det.duration,
        countScore: det.countScore,
        remark: det.remark,
        passScore: det.passScore
      };
      this.cdr.markForCheck();
    });
  }

  /*返回*/
  returnToList(): void {
    this.returnBack.emit({refesh: false, pageNo: this.currentPageNum});
  }

  start(): void {
    this.router.navigate(['/start-test'], {queryParams: {id: this.id, name: this.dataInfo.name}});
  }

  ngOnInit(): void {
    this.getDetail();
  }

  ngOnDestroy(): void {
    this.windowServe.removeStorage('radioAnswer');
    this.windowServe.removeStorage('judgeAnswer');
    this.windowServe.removeStorage('multipleAnswer');
    this.windowServe.removeStorage('standardAnswer1');
    this.windowServe.removeStorage('standardAnswer2');
    this.windowServe.removeStorage('standardAnswer3');
    this.windowServe.removeStorage('radioArray');
    this.windowServe.removeStorage('judgmentArray');
    this.windowServe.removeStorage('multipleArray');
  }
}
